<?php
class Data_barang_model extends CI_Model {
	private $primary_key = 'id_barang';
	private $table_name = 'barang';

	function __construct(){
		parent::__construct();
	}
	
	function data_getall() {
		$this->db->select('*');
		$this->db->from('bahan_gudang');
		$this->db->join('satuan', 'bahan_gudang.id_satuan = satuan.id_satuan');
		return $this->db->get();
	}
	
	function getAll() {
		$this->db->select('*');
		$this->db->from('barang');
		$this->db->join('kategori_barang', 'barang.id_kategori_barang = kategori_barang.id_kategori_barang' );
		$this->db->order_by($this->primary_key, "asc"); 
		return $this->db->get();
	}
	
	function getKategori($id) {
		$this->db->select('*');
		$this->db->where_not_in('id_kategori_barang', $id);
		$this->db->from('kategori_barang');
		return $this->db->get();
	}
	
	function get_by_id($id){
		$this->db->select('*');
		$this->db->from('barang');
		$this->db->join('kategori_barang', 'barang.id_kategori_barang = kategori_barang.id_kategori_barang' );
		$this->db->where('id_barang',$id);
		$this->db->order_by($this->primary_key, "asc"); 
		return $this->db->get();
	}		
	function get_langkah_by_id($id) {
		$this->db->where($this->primary_key,$id);
		return $this->db->get('langkah_kerja_produksi');
	}
	function get_bahan_by_id($id) {
		$this->db->where($this->primary_key,$id);
		$this->db->join('bahan_gudang', 'bahan_gudang.id_bahan = kebutuhan.id_bahan');
		return $this->db->get('kebutuhan');
	}
	function getName() {
		$this->db->select('nama_barang');
		return $this->db->get($this->table_name);
	}
	function get_id_barang($nama) {
		$this->db->select($this->primary_key);
		$this->db->like('nama_barang',$nama);
		$this->db->limit(1);
		return $this->db->get($this->table_name);
	}
	function cek_id($id) {
		$this->db->like($this->primary_key,$id);
		return $this->db->get($this->table_name);
	}
	function cek_name($name) {
	$this->db->like('nama_barang',$name,'none');
	return $this->db->get($this->table_name);
	}
	function count_all(){
	return $this->db->count_all($this->table_name);
	}
		
	function save($barang){
	$this->db->insert($this->table_name,$barang);
//	return $this->db->insert_id();
	}
	function save_langkah($barang){
	$this->db->insert('langkah_kerja_produksi',$barang);
//	return $this->db->insert_id();
	}	
	function save_kebutuhan($barang){
	$this->db->insert('kebutuhan',$barang);
//	return $this->db->insert_id();
	}	
	function update($id,$barang){
	$this->db->where($this->primary_key,$id);
	$this->db->update($this->table_name,$barang);
	}
	
	function delete($id){
	$this->db->where($this->primary_key,$id);
	$this->db->delete($this->table_name);
	}
}